package zirao.paylib;

import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

	private static final String TAG = WXPayEntryActivity.class.getSimpleName();

	private IWXAPI wxAPI;
	
	public WXPayEntryActivity() {
		
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i(PayUtil.WXPAY_LOG, "into weixin return activity");
		try {
			String wxAppid = PayCache.getInstance().wxAppId;
			if (wxAppid != null && wxAppid.length()>0) {
				wxAPI = WXAPIFactory.createWXAPI(this, PayCache.getInstance().wxAppId);
				wxAPI.handleIntent(getIntent(), this);			
			} else {
				Log.i(PayUtil.WXPAY_LOG, "Entry 初始化 Error: "+ wxAppid + " 不合法");
				if (Pay.payCallBack != null) {
					PayResult payResult = new PayResult();
					payResult.setResult(PayResult.RESULT_FAIL);
					payResult.setErrorDetail("wxAppId 不合法");
					payResult.setErrorDetail("wxAppId 不合法");
					Pay.payCallBack.done(payResult);
				}
				finish();
			}
		} catch (Exception e) {
			Log.i(PayUtil.WXPAY_LOG, "Entry 初始化 Error：" + e.getMessage() == null?"未知错误，初始化回调入口失败":e.getMessage());
			if (Pay.payCallBack != null) {
				PayResult payResult = new PayResult();
				payResult.setResult(PayResult.RESULT_FAIL);
				payResult.setErrorDetail("微信回调入口初始化失败");
				payResult.setErrorDetail("微信回调入口初始化失败");
				Pay.payCallBack.done(payResult);
			}
			finish();
		}
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		try {
			this.setIntent(intent);
			if (this.wxAPI != null) {
				this.wxAPI.handleIntent(intent, this);
			}
		} catch (Exception e) {
			if (Pay.payCallBack != null) {
				PayResult payResult = new PayResult();
				payResult.setResult(PayResult.RESULT_FAIL);
				payResult.setErrorDetail("微信回调入口初始化失败");
				payResult.setErrorDetail("微信回调入口初始化失败");
				Pay.payCallBack.done(payResult);
			}
			e.printStackTrace();
			this.finish();
		}
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		Log.i(PayUtil.WXPAY_LOG, "step 3：接收微信回调；onPayFinish, errCode = " + resp.errCode);
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			PayResult payResult = new PayResult();
			switch (resp.errCode) {
			case 0:// 成功
				payResult.setResult(PayResult.RESULT_SUCCESS);
				payResult.setResultDetail("支付成功");
				payResult.setErrorDetail("支付成功");
				break;
			case -1:// 错误，可能原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等。
				payResult.setResult(PayResult.RESULT_FAIL);
				payResult.setErrorDetail("错误，可能原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等。");
				payResult.setErrorDetail("错误，可能原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等。");
				break;
			case -2:// 用户取消
				payResult.setResult(PayResult.RESULT_CANCEL);
				payResult.setResultDetail("用户取消支付");
				payResult.setErrorDetail("用户取消支付");
				break;
			case -3:// 发送失败
				payResult.setResult(PayResult.RESULT_FAIL);
				payResult.setResultDetail("发送失败：" + resp.errStr);
				payResult.setErrorDetail("发送失败：" + resp.errStr);
				break;
			case -4:// 发送被拒绝
				payResult.setResult(PayResult.RESULT_FAIL);
				payResult.setResultDetail("发送被拒绝：" + resp.errStr);
				payResult.setErrorDetail("发送被拒绝：" + resp.errStr);
				break;
			case -5:// 不支持
				payResult.setResult(PayResult.RESULT_FAIL);
				payResult.setResultDetail("不支持错误：" + resp.errStr);
				payResult.setErrorDetail("不支持错误：" + resp.errStr);
				break;
			default:
				payResult.setErrorDetail("支付失败：" + resp.errStr);
				payResult.setErrorDetail("支付失败：" + resp.errStr);
				break;
			}
			if (Pay.payCallBack != null) {
				Pay.payCallBack.done(payResult);
			}
		}
		finish();
	}
	
    protected void onDestroy() {
        super.onDestroy();
        if(this.wxAPI != null) {
            this.wxAPI.detach();
        }
    }
}